import requests
from bs4 import BeautifulSoup
import csv


class Info:
    def __init__(self, id, picPath, name, price):
        self.id = id
        self.picPath = picPath
        self.name = name
        self.price = price


def slove(firstGood):
    return Info(firstGood.get('data-follow-id').lstrip('p'),
                firstGood.find('div', class_='pic-box SP')['data-rel'],
                firstGood.find('div', class_='pro-intro').find('h3').a.get_text(),
                firstGood.find('div', class_='price-box').find('b', class_='price-type').get_text())


def get_html(url):
    # 模拟浏览器访问
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54',
        'accept-language': 'zh-CN,zh;q=0.9'
    }
    print("--> 正在获取网站信息")
    response = requests.get(url, headers=headers)  # 请求访问网站
    if response.status_code == 200:
        html = response.text  # 获取网页源码
        return html  # 返回网页源码
    else:
        print("获取网站信息失败！")


if __name__ == '__main__':
    csv_file = open('CPU.csv', 'w', newline='', encoding='utf-8')
    writer = csv.writer(csv_file)
    writer.writerow(['ID', 'PicPath', 'Name', 'Price'])

    for page in range(21):
        # 搜索地址
        page += 1
        if page is 1:
            search_url = 'https://detail.zol.com.cn/cpu/new_pic.html'
        else:
            search_url = 'https://detail.zol.com.cn/cpu/new_pic_' + str(page) + '.html'

        html = get_html(search_url)
        soup = BeautifulSoup(html, 'lxml')
        firstGood = soup.find('div', class_='list-item item-one clearfix')
        goodsList = soup.find_all('div', class_='list-item clearfix')
        writer.writerow([slove(firstGood).id, slove(firstGood).picPath, slove(firstGood).name, slove(firstGood).price])
        for li in goodsList:
            writer.writerow([slove(li).id, slove(li).picPath, slove(li).name, slove(li).price])
    csv_file.close()
